Version Française English Version

Planète Casio - Programmes, jeux et cours pour calculatrices Casio

Bookmark and Share
Bienvenue sur Planète Casio, n'hésitez pas à poser vos questions sur le forum, et pensez à rejoindre Planète-Casio sur Facebook !
Spaceraider
Forums Casio - Problèmes de programmation ou avec un programme

Index du Forum | Problèmes de programmation ou avec un programme | [LuaFX] setmetatable does not work
Mfisch
Hors ligne
Membre
Niveau: Confirmé
Points: 189
Défis: 0
Email | Message
Posté le 06/01/2012 17:58

[LuaFX] setmetatable does not work:

When I run this program:

local print = graydraw.print

class = function(prototype)
    local derived = {}
    local derivedMT = {
        __index = prototype,
        __call  = function(proto, ...)
            local instance = {}
            local instanceMT = {
                __index = derived,
                __call = function()
                    return nil, "ERROR: Attempt to invoke an instance of a class"
                end,
            }
            setmetatable(instance, instanceMT)
            if instance.init then
                instance:init(...)
            end
            return instance
        end,
    }
    setmetatable(derived, derivedMT)
    return derived
end

car = class()

function car: init(power, type, lights, etc)
    self.power = power
    self.type = type
    self.lights = lights*2
    self.options = etc
end

jeep = car(60, "4x4", 12, {airconditioning = false,trailerhook = true})
if jeep.trailerhook then
   print("The jeep can have a trailer")
else
   print("No trailer for you ")
end


I get this error:


[string 'experiment.lua']:22: Attempt to call global 'setmetatable', a nil value


Apparently setmetatable is not a luaFX function? Could we add it to luaFX?

smileysmiley

Cliquer pour dérouler
Cliquer pour enrouler
w00t! I have a blog!

smiley
smiley





Vebveb
Hors ligne
Membre
Niveau: Elite
Points: 619
Défis: 14
Email | Message
Citer : Posté le 07/01/2012 19:09 |
I believed these functions will never be used, that's why I didn't include them.

If you believe it is necessary, I will include them.

Rq: the function is nbdraw.print and you forgot the "" for the string
----------------------------------
smiley
Mfisch
Hors ligne
Membre
Niveau: Confirmé
Points: 189
Défis: 0
Email | Message
Citer : Posté le 08/01/2012 23:44 |
Yes, if you could include this function, it would help alot.
----------------------------------
smileysmiley
Cliquer pour dérouler
Cliquer pour enrouler
w00t! I have a blog!

smiley
smiley


Vebveb
Hors ligne
Membre
Niveau: Elite
Points: 619
Défis: 14
Email | Message
Citer : Posté le 21/01/2012 18:22 |
Sorry, but I won't include it until july, because I programmed my functions assuming the user won't access metatables, then I have to look if there won't be issues if I include it.

There are many other ways to do the same thing.

Maybe you should start with an easier code.

----------------------------------
smiley
Purobaz
En ligne
Membre
Niveau: Aucun
Points: 2141
Défis: 108
Email | Message
Citer : Posté le 21/01/2012 20:34 |
I don't understand what do you want to do with this program ?
Maybe i could help you.

I'm very interested by your LuaFX librairie.
----------------------------------
smiley
smiley

Cliquer pour dérouler
Cliquer pour enrouler

graph 35+ USB

jeux :
Solitr

cours :
Solution


graph 35+ USB / 75 / 85 /95

jeux basic :
Escape prison
London 12 puro
Plumber
Yetisport ic
Platstre
M-niais
Akinator
Solitr
Robots

jeux addin :
Snowball

jeux lua :
Where am i
Mario

utilitaires basic :
Bitmap PRGM2
QR code
Boussole
Nombre en or
Calendr

utilitaires addin :
PRGM2
picture 1024

utilitaires Lua :
Pi en Lua

cours :
Solution


Prizm

jeux :
Free wheel
Yetisport puro
Rush hour color
Plumber
Prizm pack
Flight cg20

utilitaires :
Sprite maker cg
Paint 20
Menu cg20


Tutorials
PRGM2
Chaines de caractères et tableaux 2D


Projets

Ultimate solution
   73%


OBELIB
   28%


Un RPG en basic qui envoie du lourd !
   38%

Mario pour Prizm
   46%

Fight pour Prizm
   56%

addin PacMan
   90%



La programmation est pour moi la chose la plus sacrée !... c'est pour ça que j'y touche pas !
Mfisch
Hors ligne
Membre
Niveau: Confirmé
Points: 189
Défis: 0
Email | Message
Citer : Posté le 31/01/2012 20:57 |

I don't understand what do you want to do with this program ?
Maybe i could help you.

I'm very interested by your LuaFX librairie


I am trying to allow us to use lua classes like you can in lua for the TI-nspire.

If there is another way to do it then that would be great!
----------------------------------
smileysmiley
Cliquer pour dérouler
Cliquer pour enrouler
w00t! I have a blog!

smiley
smiley


Vebveb
Hors ligne
Membre
Niveau: Elite
Points: 619
Défis: 14
Email | Message
Citer : Posté le 02/02/2012 18:52 |
I don't understand completely the code you wrote,
but try with this idea:

Metatables are a sort of table associated to an objet. It is hide to the user. If the metatable is equal to nil, then the objet works like every other object. But if, for example, the case 'add' is equal to a function (and not nil), then when we add the objet with + to an other objet, it calls the function associated to add (and note the usual addition).

The simpler idea to implement it without setmetatable is to associate the objet to a metatable (not hide), and use a special function to add.

For Example



function car_add (my_car, item)
......
end

metacar = { add= car_add }
metabike = { add = bike_add }

my_car = { ....everything you want .... ; metatable = metacar }
-- my_car is explicitely associate to metacar (it doesn't copy metacar: it is a sort of pointer on metacar)

function add (item1, item2)
if (item1.metatable and item1.metatable.add)
-- it tests if metatable exist and if it has an 'add' case
then item1.metatable.add (item1, item2 )
elseif (item2.metatable and item2.metatable.add)
then item2.metatable.add(item2, item1)
else item1 + item2
end
end



With this code, you do exactly as lua do when you make item1+item2.
----------------------------------
smiley
Eiyeron
Hors ligne
Modérateur
Niveau: Confirmé
Points: 3925
Défis: 37
Email | Message
Citer : Posté le 02/02/2012 19:45 |
De quoi surcharger des opérateurs...
Mais ça déchire!
----------------------------------
smileysmileysmiley
smileysmiley
Mfisch
Hors ligne
Membre
Niveau: Confirmé
Points: 189
Défis: 0
Email | Message
Citer : Posté le 02/02/2012 21:01 |
I got my code from the lua nspire source. Could you give me alternative code that would do the same thing?
----------------------------------
smileysmiley
Cliquer pour dérouler
Cliquer pour enrouler
w00t! I have a blog!

smiley
smiley


Vebveb
Hors ligne
Membre
Niveau: Elite
Points: 619
Défis: 14
Email | Message
Citer : Posté le 03/02/2012 19:39 |
Try with this:


local print = nbdraw.print

-- uses these function instead of () and [] to access to the metatables


-- here we can use this function if item has only one argument. There is a way to extend it to any number of argument using varargs, but I don't know how to use them.

local function my_call (item, arg)
if (item.metatable and item.metatable.call)
then item.metatable.call(item,arg)
else item(arg)
end
end

local function my_index(item, i)
if (item.metatable and item.metatable.index)
then item.metatable.index(item,arg)
else item[ i ]
end
end


class = function(prototype)
local derived = {}
local derivedMT = {
index = prototype,
call = function(proto, ...)
local instance = {}
local instanceMT = {
index = derived,
call = function()
return nil, "ERROR: Attempt to invoke an instance of a class"
end,
}
instance.metatable = instanceMT
if instance.init then
instance:init(...)
end
return instance
end,
}
derived.metatable = derivedMT
return derived
end

car = class()

function car: init(power, type, lights, etc)
self.power = power
self.type = type
self.lights = lights*2
self.options = etc
end

jeep = car(60, "4x4", 12, {airconditioning = false,trailerhook = true})
if jeep.trailerhook then
print("The jeep can have a trailer")
else
print("No trailer for you" )
end
----------------------------------
smiley


Index du Forum | Problèmes de programmation ou avec un programme | [LuaFX] setmetatable does not work
Pseudo :
Adresse email :
Réponse :
 :)  ;)  :D  :p
 :lol:  8)  :(  :@
 0_0  :oops:  :grr:  :E
 :O  :sry:  :mmm:  :waza:
 :?:  :arrow:  :!:  :here:
Ajouter fichier joint :


Me prévenir par mail lorsqu'une réponse est postée.

Un petit test (entrez le résultat en chiffres):
Combien font cinq fois huit ?

Recherche :
Publicité et partenaires

Omnimaga
Casio Education
Casio Éducation
Casio Scene
Casio Scene

TI-Planet
CasioFan, la communauté ClassPad
CasioFan
Space-Raider
Space-Raider
GameMaster
GameMasters.fr

Jeux Casio - www.planet-casio.com v3.0 © créé par Neuronix et Muelsaco 2004-2012 | Il y a 68 connectés | Nous contacter | Recherches effectuées
Rugby Manager | Jeu de handball | Jeu de foot | Jeu de rugby | Jeu de tennis | Space raider | Sublinet | Top-cinema, films, cinéma, séries | Pension pour chats

Planète-Casio est un site communautaire indépendant et n'est donc pas affilié à Casio | Toute reproduction de Planète-Casio, même partielle, est interdite
Les fichiers, programmes et publications postés sur Planète-Casio restent la propriété de leurs auteurs respectifs et peuvent être soumis à des copyrights
Merci de respecter le travail des autres ! | CASIO est une marque déposée par CASIO Computer Co., Ltd